package com.jquest.editor.components;

import javax.swing.*;
import java.awt.*;
import java.awt.color.ColorSpace;
import org.newdawn.slick.Image;
import java.awt.image.*;
import java.util.Vector;
import java.nio.ByteBuffer;

/**
 * Created by IntelliJ IDEA.
 * User: Johan
 * Date: 2009-dec-12
 * Time: 15:33:17
 * To change this template use File | Settings | File Templates.
 */
public class ImageList extends JList {

    public ImageList(DefaultListModel data) {
        super(data);
        setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        setCellRenderer(new ImageListRenderer());

    }

    private java.awt.Image toAwtImage(org.newdawn.slick.Image image, boolean hasAlpha) {
		// conver the image into a byte buffer by reading each pixel in turn
		int len = 4 * image.getWidth() * image.getHeight();
		if (!hasAlpha) {
			len = 3 * image.getWidth() * image.getHeight();
		}

		ByteBuffer out = ByteBuffer.allocate(len);
		org.newdawn.slick.Color c;

		for (int y = image.getHeight()-1; y >= 0; y--) {
			for (int x = 0; x < image.getWidth(); x++) {
				c = image.getColor(x, y);

				out.put((byte) (c.r * 255.0f));
				out.put((byte) (c.g * 255.0f));
				out.put((byte) (c.b * 255.0f));
				if (hasAlpha) {
					out.put((byte) (c.a * 255.0f));
				}
			}
		}

		// create a raster of the correct format and fill it with our buffer
		DataBufferByte dataBuffer = new DataBufferByte(out.array(), len);

		PixelInterleavedSampleModel sampleModel;

		ColorModel cm;

		if (hasAlpha) {
			int[] offsets = { 0, 1, 2, 3 };
			sampleModel = new PixelInterleavedSampleModel(
					DataBuffer.TYPE_BYTE, image.getWidth(), image.getHeight(), 4,
					4 * image.getWidth(), offsets);

			cm = new ComponentColorModel(ColorSpace
					.getInstance(ColorSpace.CS_sRGB), new int[] { 8, 8, 8, 8 },
					true, false, ComponentColorModel.TRANSLUCENT,
					DataBuffer.TYPE_BYTE);
		} else {
			int[] offsets = { 0, 1, 2};
			sampleModel = new PixelInterleavedSampleModel(
					DataBuffer.TYPE_BYTE, image.getWidth(), image.getHeight(), 3,
					3 * image.getWidth(), offsets);

			cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
	                new int[] {8,8,8,0},
	                false,
	                false,
	                ComponentColorModel.OPAQUE,
	                DataBuffer.TYPE_BYTE);
		}
		WritableRaster raster = Raster.createWritableRaster(sampleModel, dataBuffer, new Point(0, 0));

		// finally create the buffered image based on the data from the texture
		// and spit it through to ImageIO
		return new BufferedImage(cm, raster, false, null);       
    }

    class ImageListRenderer extends DefaultListCellRenderer {
        public ImageListRenderer() {

        }
        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if(!(value instanceof Image)) return this;
            Image img = (Image) value;
            //if(img.getImageIcon()==null)return null;
                //setIconTextGap(90 - img.getImageIcon().getIconWidth());
            setText(img.getName());
            setIcon(new ImageIcon(toAwtImage(img, true)));
            return ImageListRenderer.this;
        }
    }
}
